package com.huluobo.a2308douyindemo.startLive

import android.util.Log
import android.view.View
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.databinding.ActivityStartLiveBinding
import com.huluobo.a2308douyindemo.mvvm.BaseActivity
import com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE
import com.tencent.live2.V2TXLiveDef
import com.tencent.live2.V2TXLivePusher
import com.tencent.live2.impl.V2TXLivePusherImpl
import com.tencent.rtmp.ui.TXCloudVideoView
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class StartLiveActivity : BaseActivity<ActivityStartLiveBinding, StartLiveViewModel>() {
    override fun initData() {
    }

    override fun initView() {
        requestPermissions(
            arrayOf(
                android.Manifest.permission.CAMERA,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 102
        )
        //推流对象,使用RTMP协议
        val mLivePusher: V2TXLivePusher = V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP)

        // 启动本地摄像头预览
        val mPusherView = findViewById<View>(R.id.pusher_tx_cloud_view) as TXCloudVideoView
        mLivePusher.setRenderView(mPusherView)
        mLivePusher.startCamera(true)
        mLivePusher.startMicrophone()

        binding.startLiveStartBtn.setOnClickListener {
            // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
            val url =
                "rtmp://222715.push.tlivecloud.com/live/123?txSecret=dc2b2837b036b05a8632e8072b4a9095&txTime=690D6CBA"
            val ret = mLivePusher.startPush(url)
            if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
                Log.i("TagA", "startRTMPPush: license 校验失败")
            }
        }

        binding.startLiveStopBtn.setOnClickListener {
            // 结束推流
            mLivePusher.stopPush()
        }
    }
}